
#include "Pawn.h"
#include <Board.h>

Pawn::Pawn(bool white): Piece(white) {
}

std::string Pawn::GetType() const{
   return "pawn";
}

std::set<BoardPosition> Pawn::GetAllMoves(Board * b, BoardPosition curPos) {
   std::set<BoardPosition> ReturnSet;

   int i = IsWhite() ? -1 : 1;

   BoardPosition pos(curPos.GetRow()+i, curPos.GetCol());
   if (pos.IsOnBoard() && b->GetPiece(pos)==NULL)
         ReturnSet.insert(pos);

   // Move twice for the first move
   if ( IsWhite() ? curPos.GetRow()== 6 : curPos.GetRow() ==1) {
      pos= BoardPosition(curPos.GetRow()+2*i, curPos.GetCol());
      if (pos.IsOnBoard() && b->GetPiece(pos)==NULL)
         ReturnSet.insert(pos);
   }

   for(int j=-1; j<2; j+=2) {
      pos = BoardPosition(curPos.GetRow()+i, curPos.GetCol()+j);
      if (pos.IsOnBoard()) {
         if (b->GetPiece(pos)!=NULL ? IsWhite() != b->GetPiece(pos)->IsWhite():false) {
            ReturnSet.insert(pos);
         }
      }
   }

   return ReturnSet;
}

std::set<BoardPosition> Pawn::GetLegalMoves(Board * b, BoardPosition curPos) {
   std::set<BoardPosition> ReturnSet;

   int i = IsWhite() ? -1 : 1;

   BoardPosition pos(curPos.GetRow()+i, curPos.GetCol());
   if (pos.IsOnBoard() && b->GetPiece(pos)==NULL)
         ReturnSet.insert(pos);

   // Move twice for the first move
   if ( IsWhite() ? curPos.GetRow()== 6 : curPos.GetRow() ==1) {
      pos= BoardPosition(curPos.GetRow()+2*i, curPos.GetCol());
      if (pos.IsOnBoard() && b->GetPiece(pos)==NULL)
         ReturnSet.insert(pos);
   }

   for(int j=-1; j<2; j+=2) {
      pos = BoardPosition(curPos.GetRow()+i, curPos.GetCol()+j);
      if (pos.IsOnBoard()) {
         if (b->GetPiece(pos)!=NULL ? IsWhite() != b->GetPiece(pos)->IsWhite():false) {
            ReturnSet.insert(pos);
         }
      }
   }

   return ReturnSet;
}
